Conditions | 6 |
Paths | 9 |
Total Lines | 71 |
Lines | 71 |
Ratio | 100 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* TODO |
||
163 | View Code Duplication | app.get(/\/[exams|e]$/, function (req, res, next) { |
|
164 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) { |
||
165 | res.status(404).send({ error: "参数不正确" }); |
||
166 | return; |
||
167 | } |
||
168 | if (fullLog) { |
||
169 | var start = new Date(); |
||
170 | console.log(`${timeStamp()} Started to query the exams: `.cyan + req.query.id.yellow); |
||
171 | } |
||
172 | access.login(req.query.id, req.query.pwd, res, function (headers) { |
||
173 | if (fullLog) { |
||
174 | console.log(`${timeStamp()} Successfully logged in.`.green); |
||
175 | } |
||
176 | |||
177 | superagent |
||
178 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list') |
||
179 | .set(headers) |
||
180 | .type('form') |
||
181 | .send({ |
||
182 | xqlbmc: '', |
||
183 | xnxqid: req.query.sem || getSem(), |
||
184 | xqlb: '' |
||
185 | }) |
||
186 | .end(function (err, iires) { |
||
187 | if (err) { |
||
188 | console.log(`${timeStamp()} Failed to reach exams page\n${err.stack}`.red); |
||
189 | res.status(404).send({ error: '获取成绩失败' }); |
||
190 | return next(err); |
||
191 | } |
||
192 | if (fullLog) { |
||
193 | console.log(`${timeStamp()} Successfully entered exams page.`.green); |
||
194 | } |
||
195 | |||
196 | let $ = cheerio.load(iires.text); |
||
197 | |||
198 | let result = { |
||
199 | name: escaper.unescape($('#Top1_divLoginName').text().match(/\s.+\(/)[0].replace(/\s|\(/g, '')), |
||
200 | id: escaper.unescape($('#Top1_divLoginName').text().match(/\(.+\)/)[0].replace(/\(|\)/g, '')), |
||
201 | sem: req.query.sem || getSem(), |
||
202 | exams: {}, |
||
203 | 'exams-count': 0, |
||
204 | }; |
||
205 | |||
206 | $('#dataList tr').each(function (index) { |
||
207 | if (index === 0) { |
||
208 | return; |
||
209 | } |
||
210 | let element = $(this).find('td'); |
||
211 | let title = escaper.unescape(element.eq(3).text()); |
||
212 | |||
213 | let item = { |
||
214 | time: escaper.unescape(element.eq(4).text()), |
||
215 | location: escaper.unescape(element.eq(5).text()), |
||
216 | seat: escaper.unescape(element.eq(6).text()) |
||
217 | }; |
||
218 | |||
219 | result.exams[title] = item; |
||
220 | result['exams-count']++; |
||
221 | }); |
||
222 | |||
223 | access.logout(headers, res, function() { |
||
224 | res.send(JSON.stringify(result)); |
||
225 | if (fullLog) { |
||
226 | console.log(`${timeStamp()} Successfully logged out: `.green + |
||
227 | req.query.id.yellow + |
||
228 | ` (processed in ${new Date() - start} ms)`.green); |
||
229 | } |
||
230 | }); |
||
231 | }); |
||
232 | }); |
||
233 | }); |
||
234 | |||
236 | console.log(`${timeStamp()} The API is now running on port ${port}. Full logging is ${fullLog ? 'enabled' : 'disabled'}`.green); |